package org.springblade.tms.module.common.base;

import org.springblade.common.utils.EnumUtils;
import org.springblade.tms.config.GlobalVariableRunner;
import org.springblade.tms.global.entity.EnumSelect;
import org.springblade.tms.global.exception.CommonException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: wzq
 * @className: EnumService
 * @createDate: 2022/3/11
 * @description: 下拉枚举查询服务
 */
@Service
@Transactional
public class EnumService extends GlobalVariableRunner {

	/***
	 * wzq
	 * 获取标记的枚举下拉
	 * @param name
	 * @return
	 */
	public EnumSelect getEnum(String name) {
		Class enumClass = GlobalVariableRunner.selectEnumMap.get(name);
		if (enumClass != null) {
			Map<String, String> selectBase = EnumUtils.enumToMap(enumClass);
			List<EnumSelect.EnumSelectItem> selectItems = new ArrayList<>(selectBase.keySet().size());
			selectBase.forEach((k, v) -> {
				selectItems.add(EnumSelect.EnumSelectItem.builder().key(k).value(v).build());
			});
			return EnumSelect.builder().keyWord(name).result(selectItems).build();
		}
		CommonException.throwCommonException("未查到数据 请输入" + GlobalVariableRunner.selectEnumMap.keySet());
		return null;
	}
}
